package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.BigScreenDao;
import cn.web.tunnelrobot.dao.ProjectDao;
import cn.web.tunnelrobot.dto.HardwareTestInfoDTO;
import cn.web.tunnelrobot.dto.ScreenProjectInfo;
import cn.web.tunnelrobot.dto.SettlAndConvergence;
import cn.web.tunnelrobot.dto.TodayWarning;
import cn.web.tunnelrobot.pojo.ImageInfo;
import cn.web.tunnelrobot.pojo.Project;
import cn.web.tunnelrobot.service.BigScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BigScreenServiceImpl implements BigScreenService {
    @Autowired
    private BigScreenDao bigScreenDao;
    @Autowired
    private ProjectDao projectDao;
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
    @Override
    public HashMap<String, Object> getProjectInfoList() {
        HashMap<String,Object> map = new HashMap<>();
        //项目id为 1
        List<ScreenProjectInfo> list = bigScreenDao.getProjectInfoList();
        for (ScreenProjectInfo screenProjectInfo :list) {
            screenProjectInfo.setTime(screenProjectInfo.getTime().substring(0,10));
        }
        List<List<ScreenProjectInfo>> listArrayList= new ArrayList<>();

        if (list.size() > 0){
            List<ScreenProjectInfo> screenProjectInfos1 = new ArrayList<>();
            List<ScreenProjectInfo> screenProjectInfos2 = new ArrayList<>();
            List<ScreenProjectInfo> screenProjectInfos3 = new ArrayList<>();
            if (list.size() <= 15 && list.size() > 10){
                for (int i = 0; i < list.size(); i++) {
                    if ( i < 5){
                        screenProjectInfos1.add(list.get(i)) ;
                    }else if(i>= 5  && i < 10){
                        screenProjectInfos2.add(list.get(i)) ;
                    }else {
                        screenProjectInfos3.add(list.get(i)) ;
                    }
                }
            }else if (list.size() <= 10 && list.size() > 5){
                for (int i = 0; i < list.size(); i++) {
                    if ( i < 5){
                        screenProjectInfos1.add(list.get(i)) ;
                    }else if(i>= 5){
                        screenProjectInfos2.add(list.get(i)) ;
                    }
                }
            }else {
                for (int i = 0; i < list.size(); i++) {
                    screenProjectInfos1.add(list.get(i)) ;
                }
            }
            if (screenProjectInfos1.size() > 0){
                listArrayList.add(screenProjectInfos1);
            }
            if (screenProjectInfos2.size() > 0){
                listArrayList.add(screenProjectInfos2);
            }
            if (screenProjectInfos3.size() > 0){
                listArrayList.add(screenProjectInfos3);
            }
        }
        map.put("code",0);
        map.put("data",listArrayList);
        return map;
    }

    @Override
    public HashMap<String, Object> getTodayWarning() {
        HashMap<String,Object> map = new HashMap<>();
        List<TodayWarning> list = bigScreenDao.getTodayWarning();

        int[] number = new int[5];
        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = true;
        boolean flag4 = true;
        boolean flag5 = true;
        for (int i = 0; i < list.size(); i++) {
//            number[i] = list.get(i).getNumber();

            if (list.get(i).getLevel().equals("Ⅰ")){
                number[0] = list.get(i).getNumber();
                flag1 = false;
            }
            if (list.get(i).getLevel().equals("Ⅱ")){
                number[1] = list.get(i).getNumber();
                flag2 = false;
            }
            if (list.get(i).getLevel().equals("Ⅲ")){
                number[2] = list.get(i).getNumber();
                flag3 = false;
            }
            if (list.get(i).getLevel().equals("Ⅳ")){
                number[3] = list.get(i).getNumber();
                flag4 = false;
            }
            if (list.get(i).getLevel().equals("Ⅴ")){
                number[4] = list.get(i).getNumber();
                flag5 = false;
            }

        }
        if (flag1) {
            number[0] = 0;
        }
        if (flag2) {
            number[1] = 0;
        }
        if (flag3) {
            number[2] = 0;
        }
        if (flag4) {
            number[3] = 0;
        }if (flag5) {
            number[4] = 0;
        }
        map.put("code",0);
        map.put("number",number);
        return map;
    }

    @Override
    public HashMap<String, Object> getTmilepost() {
        HashMap<String,Object> map = new HashMap<>();
        Project projectById = projectDao.getProjectById(1);
        if(projectById == null){
            map.put("imConstructionStatus11",0);
            map.put("imConstructionStatus12",0);
            map.put("imConstructionStatus13",0);
            map.put("imConstructionStatus14",0);
            map.put("imConstructionStatus15",0);
            map.put("imConstructionStatus21",0);
            map.put("imConstructionStatus22",0);
            map.put("imConstructionStatus23",0);
            return map;
        }
        float length = projectById.getPrTunnelLength();//隧道长度
        int imConstructionStatus14 = 0;
        int imConstructionStatus13 = 0;
        int imConstructionStatus12 = 0;
        int imConstructionStatus11 = 0;
        int imConstructionStatus21 = 0;
        int imConstructionStatus22 = 0;

        int up14 = 0;
        int up13 = 0;
        int up12 = 0;
        int up11 = 0;
        int down21 = 0;
        int down22 = 0;

        ImageInfo imageInfo1 = bigScreenDao.getTmilepost(1,1);//开挖
        ImageInfo imageInfo2 = bigScreenDao.getTmilepost(1,2);//初支
        ImageInfo imageInfo3 = bigScreenDao.getTmilepost(1,3);//二衬
        ImageInfo imageInfo4 = bigScreenDao.getTmilepost(1,4);//已完成
        ImageInfo imageInfo5 = bigScreenDao.getTmilepost(2,1);//仰拱
        ImageInfo imageInfo6 = bigScreenDao.getTmilepost(2,2);//已完成
        ImageInfo imageInfo7 = bigScreenDao.getTmilepost(1,null);//未完成（上）
        ImageInfo imageInfo8 = bigScreenDao.getTmilepost(2,null);//未完成（下）
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if(imageInfo4 != null){
            imConstructionStatus14 = imageInfo4.getImMileage();
            up14 = imageInfo4.getImMileage();
        }
       if (imageInfo3 != null){
           imConstructionStatus13 = imageInfo3.getImMileage();
           up13 = imageInfo3.getImMileage();
       }
       if (imageInfo2 != null){
           imConstructionStatus12 = imageInfo2.getImMileage();
           up12 = imageInfo2.getImMileage();
       }
        if (imageInfo1 != null){
            imConstructionStatus11 = imageInfo1.getImMileage();
            up11 = imageInfo1.getImMileage();
        }
        String format14 = decimalFormat.format(imConstructionStatus14 / length);
        map.put("imConstructionStatus14",(int)(Float.parseFloat(format14) * 100));//已完成（上半部分）
        if (up14 > up13){
            imConstructionStatus13 = 0;
            map.put("imConstructionStatus13",0);//二衬（上半部分）
        }else {
            imConstructionStatus13 = imConstructionStatus13 - imConstructionStatus14;
            String format13 = decimalFormat.format(imConstructionStatus13 / length);
            map.put("imConstructionStatus13",(int)(Float.parseFloat(format13) * 100));//二衬（上半部分）
        }
        if (up13 > up12 || up14 > up12){
            imConstructionStatus12 = 0;
            map.put("imConstructionStatus12",0);//初支（上半部分）
        }else {
            imConstructionStatus12 = imConstructionStatus12 - imConstructionStatus13 - imConstructionStatus14;
            String format12 = decimalFormat.format(imConstructionStatus12 / length);
            map.put("imConstructionStatus12",(int)(Float.parseFloat(format12) * 100));//初支（上半部分）
        }
        if (up14 > up11 || up13 > up11 || up12> up11){
            imConstructionStatus11 = 0;
            map.put("imConstructionStatus11",0);//初支（上半部分）
        }else {
            imConstructionStatus11 = imConstructionStatus11 - imConstructionStatus12 - imConstructionStatus13 - imConstructionStatus14;
            String format11 = decimalFormat.format(imConstructionStatus11 / length);
            map.put("imConstructionStatus11",(int)(Float.parseFloat(format11) * 100));//开挖（上半部分）
        }
        if (imageInfo5 != null){
            imConstructionStatus21 = imageInfo5.getImMileage();
            down21 = imageInfo5.getImMileage();
        }
        if (imageInfo6 != null){
            imConstructionStatus22 = imageInfo6.getImMileage();
            down22 = imageInfo6.getImMileage();
        }
        String format22 = decimalFormat.format(imConstructionStatus22 / length);
        map.put("imConstructionStatus22",(int)(Float.parseFloat(format22) * 100));//已完成（下半部分）
        if (down22 > down21 ){
            map.put("imConstructionStatus21",0);//仰拱（下半部分）
        }else {
            imConstructionStatus21 = imConstructionStatus21 - imConstructionStatus22;
            String format21 = decimalFormat.format(imConstructionStatus21 / length);
            map.put("imConstructionStatus21",(int)(Float.parseFloat(format21) * 100));//仰拱（下半部分）
        }
        if (imageInfo7 != null){
            String format15 = decimalFormat.format((length - imageInfo7.getImMileage()) / length);
            map.put("imConstructionStatus15",(int)(Float.parseFloat(format15) * 100));//未完成（上半部分）
        }else {
            map.put("imConstructionStatus15",0);//未完成（上半部分）
        }
        if (imageInfo8 != null){
            String format23 = decimalFormat.format((length - imageInfo8.getImMileage()) / length);
            map.put("imConstructionStatus23",(int)(Float.parseFloat(format23) * 100));//未完成（下半部分）
        }else {
            map.put("imConstructionStatus23",0);//未完成（下半部分）
        }

        return map;
    }

    @Override
    public HashMap<String, Object> getSettlAndConvergence() {
        HashMap<String,Object> map = new HashMap<>();
        List<SettlAndConvergence> list = bigScreenDao.getSettlAndConvergence();
        float[] htDailySettl = new float[5];//沉降
        float[] htDailyConvergence = new float[5];//收敛
        String[] time = new String[5];
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getHtDailySettl() == null){
                htDailySettl[i] = 0;
            }else {
                htDailySettl[i] = list.get(i).getHtDailySettl();
            }
            if (list.get(i).getHtDailyConvergence() == null){
                htDailyConvergence[i] = 0;
            }else {
                htDailyConvergence[i] = list.get(i).getHtDailyConvergence();
            }
            time[i] = list.get(i).getTime().substring(5);
        }
        map.put("code",0);
        map.put("htDailySettl",htDailySettl);
        map.put("htDailyConvergence",htDailyConvergence);
        map.put("time",time);
        return map;
    }

    @Override
    public HashMap<String, Object> getSettlAndConvergenceStraightLine() {
        HashMap<String,Object> map = new HashMap<>();
        List<Float> htCumulativeSettlMAX = new ArrayList<>();//沉降
        List<Float> htCumulativeConvergenceMAX = new ArrayList<>();//收敛
        List<Float> htCumulativeSettl = new ArrayList<>();//沉降
        List<Float> htCumulativeConvergence = new ArrayList<>();//收敛

        Integer htCumulativeSettlMAX1 = null;
        Integer htCumulativeConvergenceMAX1 = null;
        Integer htCumulativeSettl1 = null;
        Integer htCumulativeConvergence1 = null;

        //沉降（ht_mileage最大值）
        List<HardwareTestInfoDTO> list1 = bigScreenDao.getSettlAndConvergenceStraightLineMax(0);
        //收敛（ht_mileage最大值）
        List<HardwareTestInfoDTO> list2 = bigScreenDao.getSettlAndConvergenceStraightLineMax(1);
        //沉降（ht_mileage第二大值）
        List<HardwareTestInfoDTO> list3 = bigScreenDao.getSettlAndConvergenceStraightLine(0);
        //收敛（ht_mileage第二大值）
        List<HardwareTestInfoDTO> list4 = bigScreenDao.getSettlAndConvergenceStraightLine(1);
        if (list1.size() > 0){
            htCumulativeSettlMAX1 = list1.get(0).getHtMileage();
            for(int i = 0; i < list1.size();i++){
//                htCumulativeSettlMAX[i] = list1.get(i).getHtCumulativeSettl();
                htCumulativeSettlMAX.add(list1.get(i).getHtCumulativeSettl());
            }
        }
        if (list2.size() > 0){
            htCumulativeConvergenceMAX1 = list2.get(0).getHtMileage();
            for(int i = 0; i < list2.size();i++){
//                htCumulativeConvergenceMAX[i] = list2.get(i).getHtCumulativeConvergence();
                htCumulativeConvergenceMAX.add(list2.get(i).getHtCumulativeConvergence());
            }
        }
        if (list3.size() > 0){
            htCumulativeSettl1 = list3.get(0).getHtMileage();
            for(int i = 0; i < list3.size();i++){
//                htCumulativeSettl[i] = list3.get(i).getHtCumulativeSettl();
                htCumulativeSettl.add(list3.get(i).getHtCumulativeSettl());
            }
        }
        if (list4.size() > 0){
            htCumulativeConvergence1 = list4.get(0).getHtMileage();
            for(int i = 0; i < list4.size();i++){
//                htCumulativeConvergence[i] = list4.get(i).getHtCumulativeConvergence();
                htCumulativeConvergence.add(list4.get(i).getHtCumulativeConvergence());
            }
        }
        map.put("code",0);
        map.put("htCumulativeSettlMAX",htCumulativeSettlMAX);
        map.put("htCumulativeConvergenceMAX",htCumulativeConvergenceMAX);
        map.put("htCumulativeSettl",htCumulativeSettl);
        map.put("htCumulativeConvergence",htCumulativeConvergence);

        map.put("htCumulativeSettlMAX1",htCumulativeSettlMAX1);
        map.put("htCumulativeConvergenceMAX1",htCumulativeConvergenceMAX1);
        map.put("htCumulativeSettl1",htCumulativeSettl1);
        map.put("htCumulativeConvergence1",htCumulativeConvergence1);
        return map;
    }
}
